//
//  GSDKRequestModel.m
//  GameSDK
//
//  Created by Johnny on 2017/7/28.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKRequestModel.h"
#import "YYKit.h"
#import "GSDKCommonMethod.h"
#import "GSDKComplatform.h"
#import "NSDictionary+Extension.h"
@implementation GSDKModel
- (id)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (NSDictionary*)jsonModel {
    NSMutableDictionary * dic = [[self modelToJSONObject] mutableCopy];
    [dic setObject:[GSDKCOMMONMETHOD generateSignKey:[(NSDictionary *)[self modelToJSONObject] compare]] forKey:@"sign"];
    return dic;
}

//- (NSDictionary*)jsonModel {
//    NSMutableDictionary * dic = [[self modelToJSONObject] mutableCopy];
//    [dic setObject:[GSDKCOMMONMETHOD generateSignKey:[(NSDictionary *)[self modelToJSONObject] compare]] forKey:@"sign"];
//    NSLog(@"dic = %@", dic);
//    NSMutableDictionary * values = [NSMutableDictionary new];
//    for (NSString * key in dic) {
//        NSString * value = [[NSString stringWithFormat:@"%@", dic[key]] stringByURLEncode];
//        values[key] = value;
//    }
//    NSLog(@"values = %@", values);
//    return values;
//}

@end

@implementation GSDKSDKBaseModel
- (id)init {
    self = [super init];
    if (self) {
        self.cid = GSDKCOMMONMETHOD.GSDKCid;
        self.gid = GSDKINITCONFIGURE.gid;
        self.ext = [GSDKCommonMethod getDeviceMSG] == nil ? @"" : [GSDKCommonMethod getDeviceMSG];
    }
    return self;
}
@end

@implementation GSDKOtherBaseModel
- (id)init {
    self = [super init];
    if (self) {
        self.gid = GSDKINITCONFIGURE.gid;
        
    }
    return self;
}
@end

#pragma mark - GSDKGameVersionModel
@implementation GSDKGameVersionModel
- (id)init {
    self = [super init];
    if (self) {
        self.cid = GSDKCOMMONMETHOD.GSDKCid;
        self.action = @"game_version";
        self.type = @"2";
        self.ver = GSDKINITCONFIGURE.version;
    }
    return self;
}
@end

#pragma mark --- GSDKActiveDevice
@implementation GSDKActiveDeviceModel
- (id)initWithToken:(NSString *)token {
    self = [super init];
    if (self) {
        self.action = @"active";
        self.type = @"2";
        self.device_token = token;
    }
    return self;
}
@end

#pragma mark --- GSDKGetMenuTestModel
@implementation GSDKGetMenuModel
- (id)initWithToken:(NSString *)token {
    self = [super init];
    if (self) {
        self.action = @"get_menu";
        self.token = token;
    }
    return self;
}
@end

#pragma mark --- GSDKLoginModel
@implementation GSDKLoginModel
- (id)initWithUserName:(NSString *)username
          withPassword:(NSString *)password
             withToken:(NSString *)token
              withType:(NSString *)type
             withOther:(NSString *)other {
    self = [super init];
    if (self) {
        self.action = @"login";
        self.user_name = username;
        self.password = password;
        self.token = token;
        self.type = type;
        self.other = other;
    }
    return self;
}
@end

#pragma mark --- GSDKRegisterModel
@implementation GSDKRegisterModel
- (id)initWithUserName:(NSString *)username
          withPassword:(NSString *)password
             withToken:(NSString *)token
              withType:(NSString *)type {
    self = [super init];
    if (self) {
        self.action = @"reg";
        self.user_name = username;
        self.password = password;
        self.token = token;
        self.type = type;
        self.other = @"";
    }
    return self;
}
@end

#pragma mark --- GSDKGetNameModel
@implementation GSDKGetNameModel
- (id)initWithToken:(NSString *)token {
    self = [super init];
    if (self) {
        self.action = @"get_name";
        self.token = token;
    }
    return self;
}
@end

@implementation GSDKLTBaseModel
- (id)init {
    self = [super init];
    if (self) {
        self.login_token = GSDKCOMMONMETHOD.accountModel.token == nil ? @"" : GSDKCOMMONMETHOD.accountModel.token;
    }
    return self;
}
@end

@implementation GSDKGameOnlineTimeModel
- (id)initWithToken:(NSString *)token
  andGameOnlineTime:(NSString *)game_online_time {
    self = [super init];
    if (self) {
        self.action = @"game_online_time";
        self.token = token;
        self.game_online_time = game_online_time;
    }
    return self;
}
@end

#pragma mark --- GSDKUserLogoutModel
@implementation GSDKLogoutModel
- (id)initWithToken:(NSString *)token {
    self = [super init];
    if (self) {
        self.action = @"logout";
        self.token = token;
    }
    return self;
}

@end

#pragma mark- -- GSDKSelectServerModel
@implementation GSDKSelectServerModel
- (id)initWithToken:(NSString *)token
         andGameExt:(NSString *)gameExt {
    self = [super init];
    if (self) {
        self.action = @"game_server";
        self.token = token;
        self.game_ext = gameExt;
    }
    return self;
}

@end

@implementation GSDKGoodsConfigtModel
- (id)initWithToken:(NSString *)token {
    self = [super init];
    if (self) {
        self.action = @"goods_config";
        self.token = token;
    }
    return self;
}
@end

@implementation GSDKIAPCallBackModel
- (id)initIAPCallBackWithSid:(NSString *)sid
                  andReceipt:(NSString *)receipt
                    andToken:(NSString *)token
              andGameOrderId:(NSString *)game_order_id
                andGameOther:(NSString *)game_other
                  andSandbox:(NSString *)sandbox
                  andGoodsId:(NSString *)goods_id
                andGoodsName:(NSString *)goods_name
               andGoodsPrice:(NSString *)goods_price
                 andPayMoney:(NSString *)pay_money
               andLoginToken:(NSString *)login_token {
    self =[super init];
    if (self) {
        self.action = @"callback";
        self.gid = GSDKINITCONFIGURE.gid;
        self.cid = GSDKCOMMONMETHOD.GSDKCid;
        self.sid = sid;
        self.receipt = receipt;
        self.token = token;
        self.game_order_id = game_order_id;
        self.game_other = game_other;
        self.sandbox = sandbox;
        self.goods_id = goods_id;
        self.goods_name = goods_name;
        self.goods_price = goods_price;
        self.pay_money = pay_money;
        self.login_token = login_token;
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.action forKey:@"action"];
    [encoder encodeObject:self.cid forKey:@"cid"];
    [encoder encodeObject:self.gid forKey:@"gid"];
    [encoder encodeObject:self.sid forKey:@"sid"];
    [encoder encodeObject:self.receipt forKey:@"receipt"];
    [encoder encodeObject:self.token forKey:@"token"];
    [encoder encodeObject:self.game_order_id forKey:@"game_order_id"];
    [encoder encodeObject:self.game_other forKey:@"game_other"];
    [encoder encodeObject:self.sandbox forKey:@"sandbox"];
    [encoder encodeObject:self.goods_id forKey:@"goods_id"];
    [encoder encodeObject:self.goods_name forKey:@"goods_name"];
    [encoder encodeObject:self.goods_price forKey:@"goods_price"];
    [encoder encodeObject:self.pay_money forKey:@"pay_money"];
    [encoder encodeObject:self.login_token forKey:@"login_token"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (self) {
        self.action = [decoder decodeObjectForKey:@"action"];
        self.cid = [decoder decodeObjectForKey:@"cid"];
        self.gid = [decoder decodeObjectForKey:@"gid"];
        self.sid = [decoder decodeObjectForKey:@"sid"];
        self.receipt = [decoder decodeObjectForKey:@"receipt"];
        self.token = [decoder decodeObjectForKey:@"token"];
        self.game_order_id = [decoder decodeObjectForKey:@"game_order_id"];
        self.game_other = [decoder decodeObjectForKey:@"game_other"];
        self.sandbox = [decoder decodeObjectForKey:@"sandbox"];
        self.goods_id = [decoder decodeObjectForKey:@"goods_id"];
        self.goods_name = [decoder decodeObjectForKey:@"goods_name"];
        self.goods_price = [decoder decodeObjectForKey:@"goods_price"];
        self.pay_money = [decoder decodeObjectForKey:@"pay_money"];
        self.login_token = [decoder decodeObjectForKey:@"login_token"];
    }
    return self;
}
@end

@implementation GSDKWebModel
- (id)initIAPCallBackWithSid:(NSString *)sid
                    andToken:(NSString *)token
              andLogin_token:(NSString *)login_token {
    self = [super init];
    if (self) {
        self.gid = GSDKINITCONFIGURE.gid;
        self.cid = GSDKCOMMONMETHOD.GSDKCid;
        self.sid = sid;
        self.token = token;
        self.login_token = login_token;
        self.language = [NSString stringWithFormat:@"%u", GSDKCOMMONMETHOD.language];
    }
    return self;
}

- (NSDictionary*)jsonModel {
    NSMutableDictionary * dic = [[self modelToJSONObject] mutableCopy];
    [dic setObject:[GSDKCOMMONMETHOD generateSignKey:[(NSDictionary *)[self modelToJSONObject] compare]] forKey:@"sign"];
    return dic;
}

- (NSString *)getUrlParameter {
    NSString * string = @"";
    NSDictionary * dic = [self jsonModel];
    for (NSString * key in dic) {
        NSString * value = dic[key];
        string = [NSString stringWithFormat:@"%@&%@", string, key];
        string = [NSString stringWithFormat:@"%@=%@", string, [value stringByURLEncode]];
    }
    string = [string substringWithRange:NSMakeRange(1, string.length - 1)];
    return string;
}

@end

@implementation GSDKWebPayModel
- (id)initIAPCallBackWithSid:(NSString *)sid
                    andToken:(NSString *)token
              andGameOrderId:(NSString *)game_order_id
                andGameOther:(NSString *)game_other
                  andSandbox:(NSString *)sandbox
                  andGoodsId:(NSString *)goods_id
                andGoodsName:(NSString *)goods_name
               andGoodsPrice:(NSString *)goods_price
                 andPayMoney:(NSString *)pay_money
               andLoginToken:(NSString *)login_token {
    self =[super init];
    if (self) {
        self.gid = GSDKINITCONFIGURE.gid;
        self.cid = GSDKCOMMONMETHOD.GSDKCid;
        self.sid = sid;
        self.ext = [GSDKCommonMethod getDeviceMSG] == nil ? @"" : [GSDKCommonMethod getDeviceMSG];
        self.token = token;
        self.game_order_id = game_order_id;
        self.game_other = game_other;
        self.sandbox = sandbox;
        self.goods_id = goods_id;
        self.goods_name = goods_name;
        self.goods_price = goods_price;
        self.pay_money = pay_money;
        self.login_token = login_token;
        
    }
    return self;
}

- (NSDictionary*)jsonModel {
    NSMutableDictionary * dic = [[self modelToJSONObject] mutableCopy];
    [dic setObject:[GSDKCOMMONMETHOD generateSignKey:[(NSDictionary *)[self modelToJSONObject] compare]] forKey:@"sign"];
    return dic;
}

- (NSString *)getUrlParameter {
    NSString * string = @"";
    NSDictionary * dic = [self jsonModel];
    for (NSString * key in dic) {
        NSString * value = dic[key];
        string = [NSString stringWithFormat:@"%@&%@", string, key];
        string = [NSString stringWithFormat:@"%@=%@", string, [value stringByURLEncode]];
    }
    string = [string substringWithRange:NSMakeRange(1, string.length - 1)];
    return string;
}

@end

#pragma mark --- GSDKResponseModel
@implementation GSDKResponseModel
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"code" : @"code",
             @"msg" : @"msg"};
}

@end
